
#include <BImage>
#include <BStyle>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BGridLayout>
#include <BTreeItem>
#include <BListItem>
#include <BReadWrite>
#include <BButtonGroup>

#include "ContentBoard.h"

ContentBoard::ContentBoard(BWidget* parent) : BGroupBox(parent)
{
	this->setTitle("Content");
	this->setWired(false);

	BHBoxLayout* buttonLayout = new BHBoxLayout(this, Part_Top);
	{
		_lightRadioBox = new BRadioBox();
		_lightRadioBox->setName("light");
		_lightRadioBox->setForm(Form_IconAlone);
		_lightRadioBox->setColor(250, 250, 250);
		_lightRadioBox->setChecked(true);

		_darkRadioBox = new BRadioBox();
		_darkRadioBox->setName("dark");
		_darkRadioBox->setForm(Form_IconAlone);
		_darkRadioBox->setColor(0, 0, 0);

		BButtonGroup* buttonGroup = new BButtonGroup();
		buttonGroup->addButton(_lightRadioBox);
		buttonGroup->addButton(_darkRadioBox);
		connect(buttonGroup, Signal_Clicked, &ContentBoard::slotButtonGroupClicked);

		buttonLayout->addSpring();
		buttonLayout->addWidget(_lightRadioBox);
		buttonLayout->addSpacer(2);
		buttonLayout->addWidget(_darkRadioBox);
		buttonLayout->addSpacer(2);
	}

	_label = new BLabel();
	_label->setAlign(Align_Center);
	_label->setImageSize(-1, -1);

	BLayout* vlayout = new BLayout(this);
	vlayout->setWidget(_label);

	connect(this, Signal_AssetChanged, &ContentBoard::slotAssetChanged);
}
ContentBoard::~ContentBoard()
{

}

BSize ContentBoard::sizeHint() const
{
	return BSize(200, 100);
}

bool ContentBoard::checkImage(const BEntry* entry) const
{
	if (entry->attrib() & Attrib_File)
	{
		BString name = entry->name().ext();
		if (name == "png")
			return true;
		if (name == "jpg" || name == "jpeg")
			return true;
		if (name == "tif" || name == "tiff")
			return true;
		if (name == "bmp")
			return true;
	}
	return false;
}

void ContentBoard::slotAssetChanged(BObject* object, const BValue& value)
{
	if (BTreeItem* item = value)
	{
		if (const BEntry* entry = item->asset())
		{
			if (checkImage(entry))
			{
				BImage* image = ReadFile<BImage>(entry->path());
				_label->setImage(image);
				return;
			}
		}
	}
	_label->setImage(0);
}
void ContentBoard::slotButtonGroupClicked(BObject* object, const BValue& value)
{
	BButton* button = value;
	if (button->checked())
	{
		BStyle* style = _label->style();
		if (style == 0)
			style = new BStyle();
		style->setColor(Color_Background, button->color());
		_label->setStyle(style);
	}
}
